import java.util.Stack;

/**
 * Created with IntelliJ IDEA.
 * Description:
 * User: 23735
 * Date: 2022-09-27
 * Time: 22:22
 */
public class Main {
    public boolean validateStackSequences(int[] pushed, int[] popped) {
        Stack<Integer> stack=new Stack<>();
        int j=0;
        for (int i = 0; i < pushed.length;i++) {
            //注意先入栈再判断
            if(stack.isEmpty()){
                stack.push(pushed[i]);
            }else if(stack.peek()!=popped[j]){
                stack.push(pushed[i]);
            }
            //注意这里是循环 以及判断栈是否为空
            while(!stack.isEmpty()&&stack.peek()==popped[j]){
                stack.pop();
                j++;
            }
        }
        return stack.isEmpty();
    }
}
